// pages/detail/detail.js
Page({
  data: {
    memorial: {}
  },

  onLoad(options) {
    const id = options.id;
    const list = wx.getStorageSync('memorialList') || [];
    const memorial = list.find(item => item.id === id);
    
    if (!memorial) {
      wx.showToast({ title: '数据不存在', icon: 'error' });
      return setTimeout(wx.navigateBack, 1500);
    }
    
    const now = new Date().getTime();
    const targetTime = new Date(memorial.date).getTime();
    const days = Math.ceil((targetTime - now) / (1000 * 3600 * 24));
    
    this.setData({ 
      memorial: { ...memorial, days } 
    });
  },

  deleteMemorial() {
    const { id } = this.data.memorial;
    
    wx.showModal({
      title: '确认删除',
      content: '删除后无法恢复，确定删除吗？',
      success: (res) => {
        if (!res.confirm) return;
        
        let list = wx.getStorageSync('memorialList') || [];
        list = list.filter(item => item.id !== id);
        wx.setStorageSync('memorialList', list);
        
        // 触发全局更新事件
        const app = getApp();
        app.eventBus.emit('memorial-updated');
        
        wx.showToast({ title: '删除成功' });
        setTimeout(wx.navigateBack, 1500);
      }
    });
  }
});
